using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Poka_Yoke.Analysis
{
    public static class FileHelper
    {
        public static int CalculateLineNumber(int matchLocation, string inputString)
        {
            if (matchLocation < 0)
            {
                throw new ArgumentOutOfRangeException("matchCollection");
            }

            if (string.IsNullOrEmpty(inputString))
            {
                throw new ArgumentNullException("inputString");
            }

            if (matchLocation >= inputString.Length)
            {
                throw new ArgumentOutOfRangeException("matchCollection");
            }

            int lineNumber = 1;
            
            for (int i = 1; i < matchLocation; i++)
            {
                string currentString = string.Concat(inputString[i - 1],inputString[i]);

                if (currentString == Environment.NewLine)
                {
                    lineNumber++;
                }

            }

            return lineNumber;
        }

        public static string LoadFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            if (!File.Exists(fileName))
            {
                string errorMessage = string.Format(AnalyserMessages.FileNotFound, fileName);
                throw new FileNotFoundException(errorMessage);
            }

            StreamReader fileReader = new StreamReader(File.OpenRead(fileName));

            string result = fileReader.ReadToEnd();

            fileReader.Close();

            return result;
        }

    }
}
